package com.zhuiYun.project.api.userinfo.service.impl;

import com.zhuiYun.project.api.commoditycollection.mapper.CommodityCollectionMapper;
import com.zhuiYun.project.api.handworkdiy.mapper.HandworkDiyMapper;
import com.zhuiYun.project.api.handworkdiycollection.mapper.HandworkDiyCollectionMapper;
import com.zhuiYun.project.api.login.entity.User;
import com.zhuiYun.project.api.login.mapper.UserLoginMapper;
import com.zhuiYun.project.api.userinfo.service.UserInfoService;
import com.zhuiYun.project.common.errorCode.EmErrorCode;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.util.MailUtils;
import com.zhuiYun.project.util.ObjectUtils;
import com.zhuiYun.project.util.RedisUtil;
import com.zhuiYun.project.util.VerificationCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName UserInfoServiceImpl
 * @Description TODO 用戶信息业务层实现类
 * @Author 赵笛
 * @Date 2023/4/10 14:00
 **/
@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    UserLoginMapper userLoginMapper;
    @Autowired
    CommodityCollectionMapper commodityCollectionMapper;
    @Autowired
    HandworkDiyCollectionMapper handworkDiyCollectionMapper;

    /**
     * @Author 赵笛
     * @Description TODO 发送邮件验证码
     * @Date 2023/4/10 14:06
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param mail
     */
    @Override
    public CommonResult sendMailVerificationCode(String mail,Integer operationStatus) {
        //判断邮箱是否存在
        if(ObjectUtils.isNotEmpty(mail)){
            //存在邮箱
            String title = "";
            String text = "";
            //判断当前操作类型
            String VerificationCode = VerificationCodeUtils.generateVerificationCode();
            if(operationStatus==0){
                //解绑
                title = "乐工坊：您的邮箱正在被解绑";
                text = "尊敬的用户您好，乐工坊提醒您，您正在解除"
                        + mail
                        + "的绑定，请确保当前操作为本人，您的验证码是："
                        + VerificationCode
                        + "有效时间为5分钟，请您尽快完成验证";
            }else{
                //绑定
                title = "乐工坊：您正在绑定新的邮箱";
                text = "尊敬的用户您好，乐工坊提醒您，您正在绑定："
                        + mail
                        + "为新邮箱，请确保当前操作为本人，您的验证码是："
                        + VerificationCode
                        + "有效时间为5分钟，请您尽快完成验证";
            }
            MailUtils.sendMail(mail,title,text);
            //设置五分钟后过期（300秒）
            RedisUtil.set(mail,VerificationCode,300);
            //发送成功！
            return CommonResult.ok(EmErrorCode.SAVE_OK);
        }else{
            //不存在邮箱
            return CommonResult.error(EmErrorCode.EMAIL_IS_NULL) ;
        }
    }
    /**
     * @Author 赵笛
     * @Description TODO 校验验证码
     * @Date 2023/4/10 15:43
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param mail
     * @param vcode
     */
    @Override
    public CommonResult checkMailVerificationCode(String mail, String vcode) {
        //校验邮箱和验证码是否为空
        if(ObjectUtils.isNotEmpty(mail)||ObjectUtils.isNotEmpty(vcode)){
            String trueVcode = RedisUtil.get(mail).toString();
            //校验验证码是否正确
            if(vcode.equals(trueVcode)){
                //正确
                return CommonResult.ok("操作成功");
            }else{
                //错误
                return CommonResult.error(EmErrorCode.CODE_IS_ERROR);
            }
        }else{
            //邮箱或验证码为空
            return CommonResult.error(EmErrorCode.CODE_IS_NULL);
        }
    }

    /**
     * @Author 赵笛
     * @Description TODO 修改当前用户信息
     * @Date 2023/4/10 18:42
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param user
     */
    @Override
    public CommonResult updateUserInfo(User user) {
        //校验前端传过来的对象是否存在
        if(ObjectUtils.isNotEmpty(user)){
            //存在，执行修改操作
            int i = userLoginMapper.updateByPrimaryKeySelective(user);
            if(i!=0){
                //操作成功
                return CommonResult.ok(EmErrorCode.UPDATE_OK);
            }else{
                //操作失败
                return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
            }
        }else{
            //不存在，返回错误信息
            return CommonResult.error(EmErrorCode.PARAMETER_VALIDATION_ERROR);
        }
    }
    /**
     * @Author 赵笛
     * @Description TODO 根据当前用户id查询收藏商品个数
     * @Date 2023/4/10 20:07
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param userId
     */
    @Override
    public CommonResult getCommodityCollectionCountByUserId(Integer userId) {
        //校验用户id是否为空
        if(ObjectUtils.isNotEmpty(userId)){
            //id不为空
            Integer count = commodityCollectionMapper.getCommodityCollectionCountByUserId(userId);
            return CommonResult.ok(count);
        }else{
            //id为空
            return CommonResult.error(EmErrorCode.ID_NULL);
        }
    }
    /**
     * @Author 赵笛
     * @Description TODO 根据当前用户id查询收藏diy个数
     * @Date 2023/4/11 8:53
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param userId
     */
    @Override
    public CommonResult getDiyCollectionCountByUserId(Integer userId) {
        //校验用户id是否为空
        if(ObjectUtils.isNotEmpty(userId)){
            //id不为空
            Integer count = handworkDiyCollectionMapper.getDiyCollectionCountByUserId(userId);
            return CommonResult.ok(count);
        }else{
            //id为空
            return CommonResult.error(EmErrorCode.ID_NULL);
        }
    }
    /**
     * @Author 赵笛
     * @Description TODO 根据当前用户查找浏览记录数量
     * @Date 2023/4/12 9:21
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param userId
     */
    @Override
    public CommonResult getUserHistoryCountByUserId(Integer userId) {
        //拼接一下key
        String diyKey = "diyHistory"+userId;
        String commodityKey = "commodityHistory"+userId;
        //分别查询一下diy和商品的历史记录数量
        Long diyCount = RedisUtil.lGetListSize(diyKey);
        Long commodityCount = RedisUtil.lGetListSize(commodityKey);
        //计算总数
        Long count = diyCount+commodityCount;
        //校验一下是否为空
        if(ObjectUtils.isNotEmpty(count)){
            //不为空
            return CommonResult.ok(count);
        }else {
            //为空
            return CommonResult.error(EmErrorCode.ID_NOT_OBJECT);
        }
    }
}
